const SET_TITLE = 'setTitle'
const SET_FOOTER = 'setFooter'
const CHANGE_COUNT = 'changeCount'
const SOME_MUTATION = 'SOME_MUTATION'

export default {
  namespaced: true,
  state: {
    count: 0,
    title: 'vue-demo',
    footer: 'helloVueX'
  },
  mutations: {
  // Es6语法，等同 increment:function(state){...}
    [SET_TITLE]: (state, title) => {
      state.title = title || state.title
    },
    [SET_FOOTER] (state, payload) {
    // 这里的 `state` 对象是模块的局部状态
      state.footer = payload
    },
    [CHANGE_COUNT] (state, payload) {
      console.log('changeCount:', payload)
      state.count = payload
    },
    // 可以使用 ES2015 风格的计算属性命名功能来使用一个常量作为函数名 [SOME_MUTATION]
    [SOME_MUTATION] (state) {
      console.log('SOME_MUTATION:', SOME_MUTATION.length)
      state.count += SOME_MUTATION.length
    }
  },
  actions: {
  // Action 提交的是 mutation 而不是直接变更状态 可以包含任意异步操作
    changeCount (context, index) {
      context.commit(CHANGE_COUNT, index)
    }
  }
}
